package com.xueyu.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.domain.Orderapply;
import com.xueyu.domain.goods.Orderapplydetail;
import org.apache.ibatis.annotations.*;
import java.util.List;

public interface OrdDao extends BaseMapper<Orderapplydetail> {
    @Select("SELECT od.*, d.*,drugs.drugnames,supplier.supplierName FROM orderapplydetail od  JOIN drug d ON od.prodId = d.DrugID JOIN drugs ON d.DrugF = drugs.drugid JOIN supplier ON d.supplier = supplier.supplierId  WHERE od.orderNo IN (${orderNos}) ORDER BY CASE WHEN od.ord = 0 THEN 0 ELSE 1 END, od.ord;")

    @Results({
            @Result(property = "orderdetailid", column = "orderDetailId"),
            @Result(property = "orderno", column = "orderNo"),
            @Result(property = "ord", column = "ord"),
            @Result(property = "prodid", column = "prodId"),
            @Result(property = "unitname", column = "unitName"),
            @Result(property = "price", column = "price"),
            @Result(property = "prodcount", column = "prodCount"),
            @Result(property = "time", column = "time"),

            @Result(property = "drug.prodcount", column = "prodCount"),
            @Result(property = "totalprice", column = "totalPrice"),
            @Result(property = "drug.DrugID", column = "DrugID"),
            @Result(property = "drug.DrugName", column = "DrugName"),
            @Result(property = "drug.DrugPic", column = "DrugPic"),
            @Result(property = "drug.DrugF", column = "drugnames"),
            @Result(property = "drug.style", column = "style"),
            @Result(property = "drug.supplier", column = "supplierName"),
            @Result(property = "drug.DrugP", column = "DrugP"),
            @Result(property = "drug.DrugM", column = "DrugM"),
            @Result(property = "drug.price", column = "price"),
            @Result(property = "drug.ctime", column = "ctime"),
            @Result(property = "drug.Gtime", column = "Gtime")
    })

    List<Orderapplydetail> selectOrderDetailWithDrugsByOrderNoList(@Param("orderNos") String orderNos);


    @Select("SELECT od.*, d.*,drugs.drugnames,supplier.supplierName FROM orderapply od  JOIN drug d ON od.orderType = d.DrugID JOIN drugs ON d.DrugF = drugs.drugid JOIN supplier ON d.supplier = supplier.supplierId  WHERE od.orderNo IN (${orderNos})")

    @Results({
            @Result(property = "orderdetailid", column = "orderDetailId"),
            @Result(property = "orderno", column = "orderNo"),
            @Result(property = "orderdate", column = "orderdate"),
            @Result(property = "personid", column = "personid"),
            @Result(property = "style", column = "style"),
            @Result(property = "c", column = "c"),
            @Result(property = "drug.prodcount", column = "c"),
            @Result(property = "totalprice", column = "totalPrice"),
            @Result(property = "drug.DrugID", column = "DrugID"),
            @Result(property = "drug.DrugName", column = "DrugName"),
            @Result(property = "drug.DrugPic", column = "DrugPic"),
            @Result(property = "drug.DrugF", column = "drugnames"),
            @Result(property = "drug.style", column = "style"),
            @Result(property = "drug.supplier", column = "supplierName"),
            @Result(property = "drug.DrugP", column = "DrugP"),
            @Result(property = "drug.DrugM", column = "DrugM"),
            @Result(property = "drug.price", column = "price"),
            @Result(property = "drug.ctime", column = "ctime"),
            @Result(property = "drug.Gtime", column = "Gtime")
    })

    List<Orderapply> selectOrderDetailWithDrugsByOrderNoList2(@Param("orderNos") String orderNos);




    @Select("<script>" +
            "SELECT DISTINCT orderNo " +
            "FROM orderapplydetail " +
            "WHERE 1=1 " +
            "<if test='orderno != null and orderno != \"\"'>" +
            "AND orderNo = #{orderno} " +
            "</if>"
            +
            "<if test='ord != null and ord != \"\"'>" +
            "AND ord = #{ord} " +
            "</if>"
            +
            "</script>")
    List<String> getOrderNoList(Page page, @Param("orderno") String orderno,@Param("ord") String ord);


    @Select("<script>" +
            "SELECT DISTINCT orderNo " +
            "FROM orderapply " +
            "WHERE 1=1 " +
            "<if test='orderno != null and orderno != \"\"'>" +
            "AND orderNo = #{orderno} " +
            "</if>"
            +
            "<if test='ord != null and ord != \"\"'>" +
            "AND personId = #{ord} " +
            "</if>"
            +
            "</script>")
    List<String> getOrderNoList2(Page page, @Param("orderno") String orderno,@Param("ord") String ord);



    @Update("<script>" +
            "UPDATE orderapplydetail " +
            "SET ord = 1 " +
            "WHERE 1=1 " +
            "<if test='orderNo != null and orderNo != \"\"'>" +
            "AND orderNo = #{orderNo} " +
            "</if>"
            +
            "</script>")
    int updateOrderNo(@Param("orderNo") String orderNo);


    @Delete("<script>" +
            "DELETE FROM orderapplydetail " +
            "WHERE 1=1 " +
            "<if test='orderNo != null and orderNo != \"\"'>" +
            "AND orderNo = #{orderNo} " +
            "</if>" +
            "</script>")
    int deleteByOrderNo(@Param("orderNo") String orderNo);

    @Delete("<script>" +
            "DELETE FROM orderapply " +
            "WHERE 1=1 " +
            "<if test='orderNo != null and orderNo != \"\"'>" +
            "AND orderNo = #{orderNo} " +
            "</if>" +
            "</script>")
    int deleteByOrderNo2(@Param("orderNo") String orderNo);



}
